import { login, getPersonalUser } from '@/api/user'
const state = {
  token: localStorage.getItem('token') || '',
  personal: null
}
const mutations = {
  // 保存token和用户信息
  Token(state, data) {
    localStorage.setItem('token', data.data.jwt)
    state.token = data.data.jwt
    state.personal = data.data.user
  },
  // 刷新页面调用重新保存用户数据
  personal(state, data) {
    state.personal = data.data
  },
  // 清除token
  removeToken(state) {
    state.token = ''
    localStorage.removeItem('token')
  },
  // 清除用户数据
  removePersonal(state) {
    state.personal = null
  }
}
const actions = {
  // 登录
  async login(store, data) {
    const res = await login(data)
    // console.log(res)
    store.commit('Token', res)
  },
  // 获取用户数据
  async personal(store) {
    const res = await getPersonalUser()
    // console.log(res)
    store.commit('personal', res)
  },
  // 退出清除数据
  logOut(store) {
    store.commit('removeToken')
    store.commit('removePersonal')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
